package com.woniuxy.ticket_cinema.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.ticket_cinema.entity.Cinema;
import com.woniuxy.ticket_cinema.exception.CinemaNameExistException;
import com.woniuxy.ticket_cinema.mapper.CinemaMapper;
import com.woniuxy.ticket_cinema.service.CinemaService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author booty
 * @since 2021-02-22
 */
@Service
public class CinemaServiceImpl extends ServiceImpl<CinemaMapper, Cinema> implements CinemaService {

    @Resource
    private CinemaMapper cinemaMapper;

    //新增电影院，如果已存在，则抛出异常
    @Override
    public void add(Cinema cinema) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",cinema.getName());
        if(cinemaMapper.selectCount(queryWrapper) == 0){
            cinemaMapper.insert(cinema);
        }else {
            throw new CinemaNameExistException();
        }
    }

    //修改电影院，如果名字重复，则抛出异常，如果与自己名字重复，则允许修改
    @Override
    public void updateCinema(Cinema cinema) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",cinema.getName());
        if(cinemaMapper.selectOne(queryWrapper).getId() != cinema.getId()){
            cinemaMapper.updateById(cinema);
        }else {
            throw new CinemaNameExistException();
        }
    }
}
